﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC.Controllers
{
    public class HangTonController : Controller
    {
        databaseDataContext db = new databaseDataContext();
        //
        // GET: /HangTon/
        [Authorize(Roles = "0")]
        public ActionResult Index()
        {
            ViewData["Items"] = from h in db.HangTons
                                select h;
            return View();
        }

        //
        // GET: /HangTon/Details/5
        [Authorize(Roles = "0")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /HangTon/Create
        [Authorize(Roles = "0")]
        public ActionResult Create()
        {
            IEnumerable<SelectListItem> listHangHoa = db.HangHoas.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listLoaiHangTon = db.LoaiHangTons.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listKhoHang = db.KhoHangs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            ViewData["ListHangHoa"] = listHangHoa;
            ViewData["ListLoaiHangTon"] = listLoaiHangTon;
            ViewData["ListKhoHang"] = listKhoHang;
            return View();
        } 

        //
        // POST: /HangTon/Create
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Create(HangTon hangton)
        {
            try
            {
                // TODO: Add insert logic here
                hangton.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.HangTons.InsertOnSubmit(hangton);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listHangHoa = db.HangHoas.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listLoaiHangTon = db.LoaiHangTons.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listKhoHang = db.KhoHangs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                ViewData["ListHangHoa"] = listHangHoa;
                ViewData["ListLoaiHangTon"] = listLoaiHangTon;
                ViewData["ListKhoHang"] = listKhoHang;
                return View();
            }
        }
        
        //
        // GET: /HangTon/Edit/5
        [Authorize(Roles = "0")]
        public ActionResult Edit(int id)
        {
            HangTon model = (from m in db.HangTons
                             where m.ID == id
                             select m).SingleOrDefault();
            IEnumerable<SelectListItem> listHangHoa = db.HangHoas.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listLoaiHangTon = db.LoaiHangTons.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listKhoHang = db.KhoHangs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            ViewData["ListHangHoa"] = listHangHoa;
            ViewData["ListLoaiHangTon"] = listLoaiHangTon;
            ViewData["ListKhoHang"] = listKhoHang;
            return View(model);
        }

        //
        // POST: /HangTon/Edit/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Edit(int id, HangTon hangton)
        {
            HangTon model = (from m in db.HangTons
                             where m.ID == id
                             select m).SingleOrDefault();
            try
            {
                // TODO: Add update logic here
                model.So_luong = hangton.So_luong;
                model.Ma_hang = hangton.Ma_hang;
                model.Ma_loai_hang_ton = hangton.Ma_loai_hang_ton;
                model.Gia_nhap_cuoi = hangton.Gia_nhap_cuoi;
                model.Gia_xuat_cuoi = hangton.Gia_xuat_cuoi;
                model.Ma_loai_hang_ton = hangton.Ma_loai_hang_ton;
                model.Ma_kho_hang = hangton.Ma_kho_hang;
                model.Ghi_chu = hangton.Ghi_chu;

                model.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listHangHoa = db.HangHoas.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listLoaiHangTon = db.LoaiHangTons.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listKhoHang = db.KhoHangs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                ViewData["ListHangHoa"] = listHangHoa;
                ViewData["ListLoaiHangTon"] = listLoaiHangTon;
                ViewData["ListKhoHang"] = listKhoHang;
                return View(model);
            }
        }

        //
        // GET: /HangTon/Delete/5
        [Authorize(Roles = "0")]
        public ActionResult Delete(int id)
        {
            var model = (from h in db.HangTons
                         where h.ID == id
                         select h).Take(1).SingleOrDefault();
            db.HangTons.DeleteOnSubmit(model);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /HangTon/Delete/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
